import React from "react";
import LlxForm from 'components/LlxForm/index'
import { Form,notification,Select,Modal} from 'antd';
import {setRemarkBacth} from 'api/index'

const TaskAllocation = (props) => {
  const {isModalVisible, setIsModalVisible,UserSpecies,list,userName,editHttp,type,statusType} = props

  const [form] = Form.useForm();

  const formItem = [
    {label: '转移负责人', name: 'manager' , span: 20 ,render:()=>{
      return   <Select
      allowClear
      optionFilterProp="userName"
      showSearch
      fieldNames={{value:'userName',label:'userName'}}
      placeholder={'请选择负责人'}
      options={UserSpecies}
    >
    </Select>
    }},
    {label: '转移创建人', name: 'createBy' , span: 20 ,render:()=>{
      return   <Select
      allowClear
      optionFilterProp="userName"
      showSearch
      fieldNames={{value:'userName',label:'userName'}}
      placeholder={'请选择创建人'}
      options={UserSpecies}
    >
    </Select>
    }},
    {label: '留言',type:'textArea', name: 'remark' , span: 20 }
  ]

  const handleOk = async () => {
    const data1 = list.filter(i=>i[statusType]==='PN'||i[statusType]==='N')
    const data2 = list.filter(i=>i[statusType]!=='PN'&&i[statusType]!=='N')
   if(data1.length) {

      if(!form.getFieldValue().createBy) {
        notification.warning({
          message: '当前选择的数据需要选择变更的创建人',
          duration:1.5
        });

        return
      }

      if(form.getFieldValue().createBy){
          await editHttp(data1.map(item => {
            return {
              createBy: form.getFieldValue().createBy,
              id: item.id
            }
          }))
  
         await setRemarkBacth(data1.map(i => {
            return {
              manager: '',
              dataId: i.id,
              type: 'LE_VIDEO',
              remark: form.getFieldValue().remark,
              statusNow: i[statusType],
              statusNext: i[statusType]
            }
          }))
      }

    }

  if(data2.length){
      if(!form.getFieldValue().manager){
        notification.warning({
          message: '当前选择的数据需要选择变更的负责人',
          duration:1.5
        })

        return
      }

      if(form.getFieldValue().manager){
        await editHttp(data2.filter(i=>i[statusType]==='PQ'||i[statusType]==='Q').length?data2.filter(i=>i[statusType]==='PQ'||i[statusType]==='Q')?.map(item => {
          return {
            qmanager: form.getFieldValue().manager,
            id: item.id
          }
        }):'')
        await editHttp(data2.filter(i=>i[statusType]==='PR'||i[statusType]==='R').length?data2.filter(i=>i[statusType]==='PR'||i[statusType]==='R')?.map(item => {
          return {
            rmanager: form.getFieldValue().manager,
            id: item.id
          }
        }):'')
        await setRemarkBacth(data2.map(i => {
          return {
            manager: form.getFieldValue().manager,
            dataId: i.id,
            type: type,
            remark: form.getFieldValue().remark,
            statusNow: i[statusType],
            statusNext: i[statusType]
          }
         }))
     }
    }

    notification.success({
      message: '转移成功',
      description: '操作人：' + userName,
      duration:1.5
    });

    form.resetFields()
    setIsModalVisible(false);
    props.getNewList()
   
   
  }

  const handleCancel = () => {
    form.resetFields()
    setIsModalVisible(false);
  };
  
  return   <Modal width={800} title="任务分配" visible={isModalVisible} onOk={handleOk} onCancel={handleCancel}>
        <LlxForm form={form} labelCol={8} formItem={formItem} />
   </Modal>

}
export default TaskAllocation